
package org.lanqiao.tjut.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.lanqiao.tjut.bean.UsersBean;
import org.lanqiao.tjut.bean.UsersBeanStu;
import org.lanqiao.tjut.bean.UsersBeanTea;
import org.lanqiao.tjut.myUtils.MyUtils;
import org.lanqiao.tjut.service.ServiceFactory;

public class UserSelectStuCou extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 设置编码
		// req.setCharacterEncoding("UTF-8"); //已经使用filter进行了编码设置
		request.setCharacterEncoding("UTF-8");

		UsersBeanTea usersBT = new UsersBeanTea();

		int Opt = MyUtils.obj2Int(request.getParameter("opt"));
		// 成绩输入为null
		String stuCouSco = null;
        //获取  学生NUM
		
		String strUnb = request.getParameter("usernum");
		usersBT.setUsernum(strUnb);
		
//		int strUnb1=MyUtils.obj2Int(request.getParameter("selectCouid"));
//		System.out.println(strUnb);
//		
//		System.out.println(strUnb1);
		
		usersBT.setUsernum(strUnb);
		
		if (Opt == 0) {
			request.removeAttribute("usersB");
			// 退课
			// 获取课程ID
			usersBT.setCouid(MyUtils.obj2Int(request.getParameter("selectCouid")));
			// 存到usersBT
			// 学生ID
			// 删除课程
			ServiceFactory.getUserInfoManagerStuServiceInstance().deleteStuCou(usersBT);
			request.removeAttribute("usersB");
		
		} else if (Opt == 1) {
			// 选课
			// 获取课程ID
			usersBT.setCouid(MyUtils.obj2Int(request.getParameter("selectCouid")));
			// 存到usersBT
			//request.getParameter  是提取页面信息
			usersBT.setCousco(stuCouSco);
			
			// 学生ID
			// 添加课程
			ServiceFactory.getUserInfoManagerStuServiceInstance().addStuCou(usersBT);	
		}
		
		//结果刷新   传回选择页面中
		List<UsersBeanStu> stuCou = ServiceFactory.getUserInfoManagerStuServiceInstance().getAllStuCou(strUnb);
		request.getSession().setAttribute("stuCou", stuCou);

		List<UsersBeanStu> stuCouNo = ServiceFactory.getUserInfoManagerStuServiceInstance().getAllStuCouNo(strUnb);
		request.getSession().setAttribute("stuCouNo", stuCouNo);
		// 清空request缓存中的对象
		request.removeAttribute("usersBT");
	
		request.getRequestDispatcher("logined/UserInfoManager/userSelectCou.jsp").forward(request, response);
		// response.sendRedirect("logined/UserInfoManager/userInfoAddDetail.jsp");

	}

}
